package com.model;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;

import com.core.utils.LocalizableString;
import com.core.utils.StringUtils;

/**
 * @uml.dependency   supplier="com.core.utils.LocalizableString"
 * @uml.dependency   supplier="com.core.utils.StringUtils"
 */
@Entity
public class KeyValue {

	private String  userInfo;
	private String  key;
	private String  value;
	private int     id;
	private boolean escaped = true;



	@Id
	@TableGenerator(allocationSize = 10, table = "Generators", pkColumnName = "generatorName", pkColumnValue = "generatorKeyValue", name = "generatorKeyValue")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "generatorKeyValue")
	public Integer getId () {

		return id;
	}



	public void setId ( final Integer value ) {

		this.id = value;
	}



	@Column(name = "keyname")
	public String getKey () {

		return key;
	}



	public void setKey ( final String key ) {

		this.key = key;
	}



	@Column(name = "keyvalue")
	public String getValue () {

		return value;
	}



	public void setValue ( final String value ) {

		this.value = value;
	}



	@Column(name = "userInfo")
	public String getUserInfo () {

		return userInfo;
	}



	public void setUserInfo ( final String userInfo ) {

		this.userInfo = userInfo;
	}



	public KeyValue() {

		this.escaped = true;
	}



	public KeyValue(final String key, final String value, final Boolean escaped) {

		this.key = key;
		this.value = value;
		this.escaped = escaped;
	}



	public KeyValue(final String key, final String[] value, final Boolean escaped, final String separator) {

		final StringBuilder stringBuilder = new StringBuilder();
		boolean separatorAdded = false;
		for ( String subValue : value ) {

			if ( separatorAdded ) {
				stringBuilder.append(String.format("%s%s", separator, subValue));
			} else {
				stringBuilder.append(subValue);
				separatorAdded = true;
			}
		}

		this.key = key;
		this.value = stringBuilder.toString();
		this.escaped = escaped;
		this.userInfo = null;
	}



	public KeyValue(final String key, final String value) {

		this.key = key;
		this.value = value;
		this.escaped = true;
		this.userInfo = null;
	}



	@Basic
	@Column(name = "anescaped")
	public boolean isEscaped () {

		return this.escaped;
	}



	public void setEscaped ( final boolean value ) {

		this.escaped = value;
	}



	@Override
	public String toString () {

		String result = "";
		if ( StringUtils.isNilOrEmpty(key) ) {
			return result;
		}

		String formattedValue = null;
		if ( StringUtils.isNilOrEmpty(value) ) {
			formattedValue = escaped ? "\"\"" : "";
		} else {
			formattedValue = String.format(escaped ? "\"%s\"" : "%s", value);
		}

		result = String.format("%s=%s", key, formattedValue);
		return result;
	}



	public String localizableDescription () {

		return LocalizableString.sharedInstance().localizable("entity.KeyValue.description");
	}
}
